import { ConfigEnv, defineConfig, loadEnv } from "vite";
import vue from "@vitejs/plugin-vue";

import { fileURLToPath, URL } from "node:url";

import AutoImport from "unplugin-auto-import/vite";
import Components from "unplugin-vue-components/vite";
import { ElementPlusResolver } from "unplugin-vue-components/resolvers"; // Element Plus 组件解析器

import { createSvgIconsPlugin } from "vite-plugin-svg-icons";
import path from "path";

// https://vite.dev/config/
export default defineConfig(({ command, mode }: ConfigEnv) => {
  // const env = loadEnv(mode, process.cwd());
  let env = {} as any;
  const isBuild = command === "build";
  if (!isBuild) {
    env = loadEnv(
      process.argv[3] === "--mode" ? process.argv[4] : process.argv[3],
      process.cwd()
    );
  } else {
    env = loadEnv(mode, process.cwd());
  }
  return {
    plugins: [
      vue(),

      createSvgIconsPlugin({
        // Specify the icon folder to be cached
        iconDirs: [path.resolve(process.cwd(), "src/assets/svg")],
        // Specify symbolId format
        symbolId: "icon-[dir]-[name]",
      }),

      AutoImport({
        imports: ["vue", "vue-router"], // 自动导入 Vue 和 Vue Router 的 API

        eslintrc: {
          enabled: true, // 启用 ESLint 配置生成
          filepath: "./.eslintrc-auto-import.json", // 生成的 ESLint 配置文件路径
        },
        resolvers: [ElementPlusResolver()],
      }),

      Components({
        resolvers: [ElementPlusResolver({ importStyle: "sass" })],
      }),
    ],
    server: {
      host: true,
      open: true,
      proxy: {
        [env.VITE_APP_BASE_API]: {
          target: "http://101.132.105.177/grcqkDevApi",
          // target: "http://192.168.13.7:8080",
          changeOrigin: true,
          rewrite: (path) =>
            path.replace(new RegExp("^" + env.VITE_APP_BASE_API), ""),
        },
      },
    },
    resolve: {
      alias: {
        // 将 '@' 别名映射到 'src' 目录，简化导入路径
        "@": fileURLToPath(new URL("./src", import.meta.url)),
      },
    },
    css: {
      preprocessorOptions: {
        scss: {
          // 按需导入自定义主题
          additionalData: `@use "@/styles/themeVar.scss" as *;`,
        },
      },
    },
    // build: {
    //   chunkSizeWarningLimit: 2000, // 消除打包大小超过500kb警告
    //   minify: "terser", // Vite 2.6.x 以上需要配置 minify: "terser", terserOptions 才能生效
    //   terserOptions: {
    //     compress: {
    //       keep_infinity: true, // 防止 Infinity 被压缩成 1/0，这可能会导致 Chrome 上的性能问题
    //       drop_console: true, // 生产环境去除 console
    //       drop_debugger: true, // 生产环境去除 debugger
    //     },
    //     format: {
    //       comments: false, // 删除注释
    //     },
    //   },
    //   rollupOptions: {
    //     output: {
    //       // manualChunks: {
    //       //   "vue-i18n": ["vue-i18n"],
    //       // },
    //       // 用于从入口点创建的块的打包输出格式[name]表示文件名,[hash]表示该文件内容hash值
    //       entryFileNames: "js/[name].[hash].js",
    //       // 用于命名代码拆分时创建的共享块的输出命名
    //       chunkFileNames: "js/[name].[hash].js",
    //       // 用于输出静态资源的命名，[ext]表示文件扩展名
    //       assetFileNames: (assetInfo: any) => {
    //         const info = assetInfo.name.split(".");
    //         let extType = info[info.length - 1];
    //         // console.log('文件信息', assetInfo.name)
    //         if (
    //           /\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/i.test(assetInfo.name)
    //         ) {
    //           extType = "media";
    //         } else if (/\.(png|jpe?g|gif|svg)(\?.*)?$/.test(assetInfo.name)) {
    //           extType = "img";
    //         } else if (/\.(woff2?|eot|ttf|otf)(\?.*)?$/i.test(assetInfo.name)) {
    //           extType = "fonts";
    //         }
    //         return `${extType}/[name].[hash].[ext]`;
    //       },
    //     },
    //   },
    // },
  };
});
